package assets
{
	import away3d.animators.SkeletonAnimationSet;
	import away3d.animators.data.Skeleton;
	import away3d.textures.BitmapTexture;
	
	import flash.display.BitmapData;
	import flash.utils.Dictionary;
	
	
	/**
	 * 全局资源管理
	 * 所有单独的资源类都在这里实例化供全局调用
	 */
	public class Asset
	{
		//资源池
		public static var resBitmapDatas:Dictionary = new Dictionary;
		public static var resBitmapTextures:Dictionary = new Dictionary;
		public static var resAtfTextures:Dictionary = new Dictionary;
		public static var resSkeletons:Dictionary = new Dictionary;
		public static var resStaticMeshs:Dictionary = new Dictionary;
		public static var resSkinnedMeshs:Dictionary = new Dictionary;
		
		//资源管理实例
		public static var bitmapdata:AssetBitmapData = new AssetBitmapData;
		public static var bitmapTexture:AssetBitmapTexture = new AssetBitmapTexture;
		public static var atfTexture:AssetAtfTexture = new AssetAtfTexture;
		public static var atfTextureWithZIP:AssetAtfTextureWhitZIP = new AssetAtfTextureWhitZIP;
		public static var skeleton:AssetSkeleton = new AssetSkeleton;
		public static var mesh:AssetMesh = new AssetMesh;
		///////////////////////////////////////////////////////////////
		
		public static function disposeAll():void
		{
			for each(var bmd:BitmapData in resBitmapDatas)bmd.dispose();
			resBitmapDatas = new Dictionary;
			
			for each(var bmt:BitmapTexture in resBitmapTextures)bmt.dispose();
			resBitmapTextures = new Dictionary;
			
			for each(var ary:Array in resStaticMeshs)
			{
				for each(var obj:Object in ary)
				{
					obj.subGeo.dispose();
					obj.mat = null;
					obj.fixalpha = null;
				}
			}
			resStaticMeshs = new Dictionary;
			
			for each(var skMesh:Object in resSkinnedMeshs)
			{
				ary = skMesh.geometrys;
				for each(obj in ary)
				{
					obj.subGeo.dispose();
					obj.mat = null;
					obj.fixalpha = null;
				}
			}
			resSkinnedMeshs = new Dictionary;
			
			for each(var skeObj:Object in resSkeletons)
			{
				var skeleton:Skeleton = skeObj.skeleton;
				var animateSets:SkeletonAnimationSet = skeObj.animationSet;
				animateSets.dispose();
				skeleton.dispose();
			}
			resSkeletons = new Dictionary;
		}
		
		public static function disposeBitmapData(key:String):void
		{
			(resBitmapDatas[key] as BitmapData).dispose();
			delete resBitmapDatas[key];
		}
		
		public static function disposeBitmapTexture(key:String):void
		{
			(resBitmapTextures[key] as BitmapTexture).dispose();
			delete resBitmapTextures[key];
		}
		
		public static function disposeStaticMesh(key:String):void
		{
			var geometrys:Array = resStaticMeshs[key];
			for each(var obj:Object in geometrys)
			{
				obj.subGeo.dispose();
				obj.mat = null;
				obj.fixalpha = null;
			}
			delete resStaticMeshs[key];
		}
		
		public static function disposeSkinnedMesh(key:String):void
		{
			var geometrys:Array = resSkinnedMeshs[key].geometrys;
			for each(var obj:Object in geometrys)
			{
				obj.subGeo.dispose();
				obj.mat = null;
				obj.fixalpha = null;
			}
			delete resSkinnedMeshs[key];
		}
		
		public static function disposeSkeleton(key:String):void
		{
			var skeleton:Skeleton = resSkeletons[key].skeleton;
			var animateSets:SkeletonAnimationSet = resSkeletons[key].animationSet;
			animateSets.dispose();
			skeleton.dispose();
			
			delete resSkeletons[key];
		}
	}
}